<template>
  <div id="login">
    <div class="login_box">
      <div class="tx">
        <img src="../assets/img/tx.jpg" alt />
      </div>
      <el-form ref="FromRef" :rules="rules" :model="loginForm">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-yonghuming" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-tianchongxing-"
            type="password"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="info" @click="resetFrom">重置</el-button>
          <el-button type="primary" @click="login">登入</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456"
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 15, message: "长度在 3 到 15 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 15, message: "长度在 3 到15 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    resetFrom() {
      this.$refs.FromRef.resetFields();
    },
    login() {
      this.$refs.FromRef.validate(async val => {
        console.log(val);
        if (val) {
          const { data: res } = await this.$http.post("login", this.loginForm);
          this.$message({
            message: "登入成功",
            type: "success"
          });
          window.sessionStorage.setItem("token", res.data.token);
          this.$router.push("/home");
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
#login {
  width: 100%;
  height: 100%;
  background: url(../assets/img/bg.jpg);
  .login_box {
    width: 530px;
    height: 350px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 10px;
    .tx {
      position: absolute;
      top: -60px;
      left: 50%;
      transform: translate(-50%);
      border: 10px solid #fff;
      border-radius: 50%;
      width: 150px;
      height: 150px;
      box-shadow: 0 0 1px #ccc;
      img {
        width: 100%;
        border-radius: 50%;
      }
    }
  }
  .el-form {
    position: absolute;
    bottom: 20px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
</style>